package com.archie.zhufengfm.fragments.home;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.archie.zhufengfm.R;
import com.archie.zhufengfm.adapters.CommonFragmentPagerAdapter;
import com.archie.zhufengfm.fragments.home.discover.DiscoverAnchorFragment;
import com.archie.zhufengfm.fragments.home.discover.DiscoverCategoryFragment;
import com.archie.zhufengfm.fragments.home.discover.DiscoverLiveFragment;
import com.archie.zhufengfm.fragments.home.discover.DiscoverRatingFragment;
import com.archie.zhufengfm.fragments.home.discover.DiscoverRecommendFragment;

import java.util.LinkedList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverFragment extends Fragment implements TabLayout.OnTabSelectedListener {


    private ViewPager pager;

    public DiscoverFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_discover, container, false);

        TabLayout tabLayout =
                (TabLayout) ret.findViewById(R.id.discover_tab_bar);

//        TabLayout.Tab tab = tabLayout.newTab().setText("推荐");
//        tabLayout.addTab(tab);
//
//        tab = tabLayout.newTab().setText("分类");
//        tabLayout.addTab(tab);
//
//        tab = tabLayout.newTab().setText("直播");
//        tabLayout.addTab(tab);
//
//        tab = tabLayout.newTab().setText("榜单");
//        tabLayout.addTab(tab);
//
//        tab = tabLayout.newTab().setText("主播");
//        tabLayout.addTab(tab);

        // 2. ViewPager 加载
        pager = (ViewPager) ret.findViewById(R.id.discover_view_pager);

        List<BaseFragment> fragments = new LinkedList<BaseFragment>();

        fragments.add(new DiscoverRecommendFragment());
        fragments.add(new DiscoverCategoryFragment());
        fragments.add(new DiscoverLiveFragment());
        fragments.add(new DiscoverRatingFragment());
        fragments.add(new DiscoverAnchorFragment());

        CommonFragmentPagerAdapter pagerAdapter =
                new CommonFragmentPagerAdapter(getChildFragmentManager(), fragments);

        pager.setAdapter(pagerAdapter);

//        // ViewPager 滑动与TabLayout绑定
//        pager.addOnPageChangeListener(
//                new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//
//        tabLayout.setOnTabSelectedListener(this);

        // 封装了 TabLayout 与 ViewPager 的联动
        // 需要ViewPager内部指定的Adapter必须重写 getPagerTitle（）方法
        tabLayout.setupWithViewPager(pager);

        return ret;
    }


    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        // TODO 切换ViewPager
        pager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
